<?php

namespace App\Http\Controllers;

use Faker\Provider\Uuid;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller
{
    //
    public function index()
    {
        return view("upload");
    }

    /**
     * 文件上传
     */
    public function uploadFile(Request $request)
    {


        if (!$request->hasFile('avatar')) { // 是否存在上传文件
            return '上传失败';
        }
        $file = $request->file('avatar');

//        $filename = $file->getFilename();
//        $path = $file->path();
        $extension = $file->extension();

        //return json_encode(array(['path' => $path, 'extension' => $extension, 'filename' => $filename]));

//        Storage::putFile('avatars', $file);

//        存储文件到指定的目录中
        $file->store('avatars');

        // 验证文件是否上传成功
        if ($request->file('avatar')->isValid()) {
            return "ok";
        }
        return 'error';
    }
}
